package org.jeecg.common.util;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 项目生成一些数据工具类
 */
public class DataUtil {

    private static final String numStr = "1234567890";

    private static final String charStr = "123456789aAbBcCdDeEfFgGhHiIjJkKmMnNpPqQrRsStTuUvVwWxXyYzZ";

    public static String randomNum(int length) {
        return random(length, false);
    }

    public static String randomStr(int length) {
        return random(length, true);
    }

    public static String randomStr(int length, String randStr) {
        return random(length, true, randStr);
    }

    public static String random(int length, boolean isStr) {
        return random(length, isStr, StringUtils.EMPTY);
    }

    /**
     *
     * @param length 随机数的长度
     * @param isStr 为false时，只会取0~9的随机数, 此时randStr失效
     * @param randStr 如果randStr不为空且isStr=true时， 取randStr中的字符
     * @return
     */
    public static String random(int length, boolean isStr, String randStr) {
        String strTable = "";
        if (!isStr) {
            strTable = numStr;
        } else {
            strTable = StringUtils.isNotBlank(randStr) ? randStr  : charStr;
        }

        int len = strTable.length();
        String retStr = "";
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            SecureRandom secureRandom = new SecureRandom();
            for (int i = 0; i < length; i++) {
//                double dblR = Math.random() * len;
//                int intR = (int) Math.floor(dblR);
//                char c = strTable.charAt(intR);
                int intR = secureRandom.nextInt(len);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }

    public static String getStrHtml(String str) {
        Matcher matcher = mathcherHtmlStr(str);
        if (matcher != null) {
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
        }
        return null;

    }

    public static Matcher mathcherHtmlStr(String str) {
        if(null == str) {
            return null;
        }

        // 去除所有的HTML标签
        String regEx = "(.*?<HEAD.*?>(.*?)<\\/HEAD>)|(<.*?>)|(<^/[a-zA-Z].*?>)";
        Pattern p_html = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        return p_html.matcher(str);
    }

    /**
     * 在设置的范围内随机生成一个数 [min, max)
     * @param min
     * @param max
     * @return
     */
//    public static int randomInt(int min, int max) {
//        return (int)(Math.random() * (max - min) + min);
//    }

//    public static long randomLong(long min, long max) {
//        return (long)(Math.random() * (max - min) + min);
//    }

    public static long randomLong(long min, long max) {
        SecureRandom secureRandom = new SecureRandom();
        long range = max - min + 1;
        // Ensure that range is positive to avoid division by zero
        if (range <= 0) {
            throw new IllegalArgumentException("Invalid range");
        }
        // Generate a random number within the specified range
        long randomNumber = secureRandom.nextLong() % range;
        // Ensure that randomNumber is positive
        if (randomNumber < 0) {
            randomNumber += range;
        }
        // Shift the random number to the specified range
        return randomNumber + min;
    }


    public static String getUUID() {
        String str = UUID.randomUUID().toString();
        return str.replace("-", "");
    }

    /**
     * 在一个范围内，随机生成一个时间，精确到秒
     *  范围： [startTime, endTime)
     * @param startTime
     * @param endTime
     * @param datePattern
     * @return
     */
    public static Date randomDate(String startTime, String endTime, String datePattern) {
        return randomDate(DateUtil.parse(startTime, datePattern), DateUtil.parse(endTime, datePattern));
    }

    public static Date randomDate(Date startTime, Date endTime) {
        long start = startTime.getTime() / 1000;
        long end = endTime.getTime() / 1000;
        long time = randomLong(start, end) * 1000;

        Date date = new Date();
        date.setTime(time);
        return date;
    }

    /**
     * 返回更大的BigDecimal
     * @author: qiang.zhou
     * @date: 2024/9/13 17:52
     * @param a
     * @param b
     * @return java.math.BigDecimal
     **/
    public static BigDecimal max(BigDecimal a, BigDecimal b) {
        return a.compareTo(b) > 0 ? a : b;
    }

    /**
     * 将int转为8字符的二进制
     * @author: qiang.zhou
     * @date: 2025/3/10 15:03
     * @param i
     * @return java.lang.String
     **/
    public static String toBin(int i) {
        return String.format("%8s", Integer.toBinaryString(i & 0xFF)).replace(' ', '0');
    }


}
